<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
echo "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>";
echo "<response>";
$ticket_no = $_GET['ticket_no'];
$film_code = $_GET['film_code'];
$ticket_rate = $_GET['ticket_rate'];
$agent_code = $_GET['agent_code'];
$action = $_GET['action'];

session_start();
if (!$ticket_no && $_SESSION['kteam_auth'] != true && !($_SESSION['agent_auth'] && !$action))
{
    $expire=time()+60*60;
    setcookie("redirector", "customer.html", $expire);
    echo "<result>Login</result>";
    echo "</response>";
    //header("Location: login.html");
    exit;
}

include("sqlconnect.php");
include("sqlgenerator.php");
$debug = false;

if ($debug) {
    echo '<debug>';
    echo 'Post parameters: ' . $_GET['agent_code'];
    echo '</debug>';
}

if (!is_null($action) && $action=='delete') {
    db_connect();
    $query = delete_customer($ticket_no);
    if ($debug) {
        echo '<debug>';
        echo 'Query: ' . $query;
        echo '</debug>';
    }
    $result = db_query($query);

    $query = delete_ticket($ticket_no);
    $result = db_query($query);
    echo "<result>Deleted</result>";
    exit;
}

db_connect();

// Query the agent details from the agent code
if (is_null($ticket_no)) {
    if (is_null($film_code)) {
        if (is_null($agent_code)) {
            $query = get_all_customers();
        } else {
            $query = get_all_customers_agent($agent_code);
        }
    } else {
        if (is_null($ticket_rate)) {
            $query = get_all_customers_film($film_code);
        } else {
            $query = get_all_customers_film_rate($film_code, $ticket_rate);
        }
    }
} else {
    $query = get_customer($ticket_no);
}
if ($debug) {
    echo '<debug>';
    echo 'Query: ' . $query;
    echo '</debug>';
}
$result = db_query($query);
if ($debug) {
    echo '<debug>';
    echo 'Query Result: ' . $result;
    echo '</debug>';
}

while ($row = mysql_fetch_array($result)) {
    echo '<customer>';
    try {
        $query1 = get_ticket($row['ticket_no']);
        $result1 = db_query($query1);
        $row1 = mysql_fetch_array($result1);

        echo '<cust_id>'.$row['id'].'</cust_id>';
        echo '<cust_name>'.$row['name'].'</cust_name>';
        echo '<centre_code>'.$row['centre_code'].'</centre_code>';
        echo '<agent_code>'.$row['agent_code'].'</agent_code>';
        echo '<film_code>'.$row['film_code'].'</film_code>';
        echo '<ticket_no>'.$row['ticket_no'].'</ticket_no>';
        echo '<phone>'.$row['phone'].'</phone>';
        echo '<date_issue>'.$row['date_issue'].'</date_issue>';
        echo '<time_issue>'.$row['time_issue'].'</time_issue>';
        echo '<ticket_rate>'.$row['ticket_rate'].'</ticket_rate>';
        echo '<value>'.$row1['value'].'</value>';
    } catch (Exception $e) {
        echo '</customer>';
        echo "<result>Error</result>";
        echo "</response>";
    }
    echo '</customer>';
}
echo "<result>Success</result>";
echo "</response>";

?>